package MultiMediaPlayer.Media.Playlist;

import MultiMediaPlayer.Media.Media;

import java.util.ArrayList;


//组合模式
public class PlayList implements Media {
    ArrayList<Media> mediaList = new ArrayList<Media>();

    public PlayList() {}

    public void addMedia(Media media) {
        mediaList.add(media);
    }

    public void removeMedia(Media media) {
        mediaList.remove(media);
    }

    public ArrayList<Media> getMediaList() {
        return mediaList;
    }

    @Override
    public void play() {
        for (Media media : mediaList) {
            media.play();//顺序播放
            System.out.println(" ");
        }
    }

    @Override
    public String getDescription() {
        return "播放列表如下：\n" +
               mediaList.stream()
                        .map(Media::getDescription)
                        .reduce(" ", (desc, item) -> desc + item + "\n");
        //以上代码是使用Java 8的Stream API来生成播放列表的描述
    }

    @Override
    public String getName() {
        for(Media media : mediaList) {
            if (media.getName() != null) {
                return media.getName();
            }
        }
        return "未知播放列表";
    }
}
